package com.cqc.manage.service.Imp.business;

import cn.hutool.core.date.DateUtil;
import com.cqc.manage.entity.CostStatistics;
import com.cqc.manage.repositories.CostStatisticsRepositories;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
@EnableScheduling
public class BusinessEchartService {
    private Logger LOG = LoggerFactory.getLogger(BusinessEchartService.class);

    @Autowired
    private CostStatisticsRepositories costStatisticsRepositories;

    @Async
    @Cacheable(cacheNames = "echart-main",key = "#str")
    public Map<String,Map<Integer,List<Float>>> getMainEchart(String str) {
        List<String> timeline = new ArrayList<>();
        Map<String,Map<Integer,List<Float>>> result = new HashMap<>();
        for (String natureName : getNature()) {
            Map<Integer,List<Float>> map = new HashMap<>();
            for (int month = 1; month <= DateUtil.month(new Date())+1; month++) {
                List<Float> natureEverybody = new ArrayList<>();
                for (String engName : getEngName()) {
                    Float sum = 0.0f;
                    List<CostStatistics> byEngeerAndAndNature = costStatisticsRepositories.findByEngeerAndAndNature(engName, natureName);
                    for (CostStatistics costStatistics : byEngeerAndAndNature) {
                        int year = DateUtil.year(new Date());
                        if (DateUtil.year(DateUtil.parse(costStatistics.getStartDate())) == year && (DateUtil.month(DateUtil.parse(costStatistics.getStartDate()))+1) == month) {
                            if (!costStatistics.getAgreementAmount().equals("")) {
                                sum += Float.valueOf(costStatistics.getAgreementAmount());
                            }
                        }
                    }
                    natureEverybody.add(sum);
                }
                map.put(month,natureEverybody);
            }
            result.put(getNatureBieName(natureName),map);
        }
        return result;
    }

    public List<String> getEngName() {
        List<String> engineerList = new ArrayList<>();
        engineerList.add("董银生");
        engineerList.add("王发娟");
        engineerList.add("黄雨吉");
        engineerList.add("江魏佳");
        engineerList.add("胡舜杰");
        engineerList.add("汪洋");
        engineerList.add("翁良汉");
        engineerList.add("杨琨");
        engineerList.add("杨远礼");
        engineerList.add("程敏");
        return engineerList;
    }

    public List<String> getNature() {
        List<String> natureList = new ArrayList<>();
        natureList.add("CCC核查");
        natureList.add("委托");
        natureList.add("CQC监督抽查");
        natureList.add("CCC监督抽查");
        natureList.add("CCC强制认证");
        natureList.add("CQC自愿认证");
        natureList.add("CQC核查");
//        natureList.add("能源效率检测");
//        natureList.add("能力验证");
        return natureList;
    }

    public String getNatureBieName(String nature){
        Map<String,String> map= new HashMap<>();
        map.put("CCC核查","dataCQCcheck");
        map.put("委托","dataCCCentrust");
        map.put("CQC监督抽查","dataCQCSSC");
        map.put("CCC监督抽查","dataCCCSSC");
        map.put("CCC强制认证","dataCCCCC");
        map.put("CQC自愿认证","dataCQCRA");
        map.put("CQC核查","dataCCCcheck");
        return map.get(nature);
    }

    /**
     * 统计每个月的协议金额
     * @return
     */
    public List<Float> getEveryMonthAgreementAmount(){

        List<Float> everyMonthAgreementAmountList = new ArrayList<>();
        everyMonthAgreementAmountList.add(0.0F);
        for(int i = 1 ; i<= DateUtil.month(new Date())+1 ;i++){
            String month = "";
            if(i < 10 ){
                month = "0"+i;
            }
            Float sum = 0.0F;
            String finalMonth = month;
            Specification<CostStatistics> specification = new Specification<CostStatistics>() {
                @Override
                public Predicate toPredicate(Root<CostStatistics> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicateList = new ArrayList<>();
                    predicateList.add(criteriaBuilder.like(root.get("startDate"), "%"+DateUtil.year(new Date())+"-"+ finalMonth +"%"));
                    predicateList.add(criteriaBuilder.notLike(root.get("client"),"%中认尚动%"));
                    return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
                }
            };
            List<CostStatistics> all = costStatisticsRepositories.findAll(specification);
            for (CostStatistics costStatistics: all){
                sum += Float.valueOf(costStatistics.getAgreementAmount());
            }
            everyMonthAgreementAmountList.add(sum);
        }
        return everyMonthAgreementAmountList;
    }



}
